package com.maxvision.mybatis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxvision.mybatis.dto.StudentDto;
import com.maxvision.mybatis.enums.ExceptionEnum;
import com.maxvision.mybatis.mapper.StudentMapper;
import com.maxvision.mybatis.service.IStudentService;
import com.maxvision.mybatis.vo.StudentVo;

import com.maxvision.mybatis.vo.UpdateStudentVo;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.*;

/**
 * 类说明
 *
 * @author HuangRongFu
 * @date 2022/10/17
 */

@Service
@Log4j2
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDto> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResponseEntity addStudent(StudentVo vo) {

        StudentDto studentDto = new StudentDto();
        BeanUtil.copyProperties(vo, studentDto, false);
        //studentDto.setCreateBy("123456");
        //studentDto.setUpdateBy("123456");
        studentMapper.insert(studentDto);

        return ResponseEntity.ok("添加成功");
    }

    @Override
    public ResponseEntity updateStudent(UpdateStudentVo vo) throws Exception {

        //if (true) {
        //
        //    throw new MyException(ExceptionEnum.FAIL);
        //}

        StudentDto studentDto = new StudentDto();
        studentDto = studentMapper.selectOne(new LambdaQueryWrapper<StudentDto>().eq(StudentDto::getId, vo.getId()));
        //BeanUtil.copyProperties(vo, studentDto);

        //studentDto.setUpdateBy("1132");

        studentMapper.updateById(studentDto);
        return ResponseEntity.ok("更新成功");
    }

    @Override
    public ResponseEntity impImage(MultipartFile[] images) throws Exception {


        try {
            List<MultipartFile> list = Arrays.asList(images);
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));

            List<List<MultipartFile>> lists = ListUtil.splitAvg(list, 10);
            CountDownLatch cd = new CountDownLatch(10);
            for (List<MultipartFile> multipartFiles : lists) {

                threadPoolExecutor.execute(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {

                        long start = System.currentTimeMillis();

                        List<String> imageStrList = new ArrayList<>();
                        for (MultipartFile multipartFile : multipartFiles) {

                            File file = new File("src/main/resources/image/" + RandomUtil.randomNumbers(10) + ".jpg");
                            FileUtil.writeBytes(multipartFile.getBytes(), file);
                        }
                        cd.countDown();
                        long end = System.currentTimeMillis();
                        System.out.println("线程处理耗时: " + (end - start) + "ms");
                    }
                });

                //String s = threadPoolExecutor.submit(new Callable<String>() {
                //    @Override
                //    public String call() throws Exception {
                //        List<String> imageStrList = new ArrayList<>();
                //        for (MultipartFile multipartFile : multipartFiles) {
                //
                //            File file = new File("src/main/resources/image/" + RandomUtil.randomNumbers(12) + ".jpg");
                //            FileUtil.writeBytes(multipartFile.getBytes(), file);
                //        }
                //        return "success";
                //    }
                //}).get();
                //log.info(s);
            }
            cd.await();

            //for (MultipartFile multipartFile : list) {
            //    File file = new File("src/main/resources/image/" + RandomUtil.randomNumbers(12) + ".jpg");
            //    FileUtil.writeBytes(multipartFile.getBytes(), file);
            //}
        } catch (Exception e) {

            e.printStackTrace();
        }


        return ResponseEntity.ok("success");
    }

    @Override
    public ResponseEntity selectList(int pageNum, int pageSize) {

        System.out.println(pageNum);
        System.out.println(pageSize);
        return null;
    }

}
